﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Giaiphongmatbang.DataModel;

namespace Giaiphongmatbang.BLL
{
    public class ManagmentsRepository
    {
        private QLGPMBEntities data = new QLGPMBEntities();

        public IEnumerable<Managment> GetAll(int pageSize, int pageNumber, ref int totalRow)
        {
            var results = from p in data.Managments
                          orderby p.Project.ProjectID
                          select p;
            return results.Skip((pageNumber - 1) * pageSize).Take(pageSize);
        }

        public Managment GetByID(int ID)
        {
            return data.Managments.FirstOrDefault(p => p.ManagementID == ID);
        }
        public IEnumerable<Managment> GetByProjectID(int ID)
        {
            return data.Managments.Where(p => p.Project.ProjectID == ID);
        }

        public void Delete(Managment m)
        {
            data.DeleteObject(m);
            data.SaveChanges();
        }

        public void Insert(Managment m)
        {
            data.AddToManagments(m);
        }

        public void Insert(int ProjectID, string FullName, string Order, string Representative)
        {
            Managment manager = new Managment
            {
                FullName = FullName,
                Order = Order,
                Representative = Representative,
                Project = data.Projects.FirstOrDefault(p=>p.ProjectID == ProjectID)
            };
            data.SaveChanges();
        }
        public void Save()
        {
            data.SaveChanges();
        }
    }
}